class Iterator {
    constructor(items) {
        this.items = items
        this.cursor = 0
    }
    has_next () {
        return this.cursor < this.items.length
    }
    next () {
        const item = this.items[this.cursor]
        this.cursor += 1
        return item
    }
}

class Collection {
    constructor() {
        this.items = []
    }
    add_item (item) {
        this.items.push(item)
    }
    iterator () {
        return new Iterator(this.items)
    }
}

const collection = new Collection()
collection.add_item('item 1')
collection.add_item('item 2')
collection.add_item('item 3')

const iterator = collection.iterator()
while (iterator.has_next()) {
    console.log(iterator.next())
}